<?php

namespace app\admin\controller;

use think\Db;

class Config extends Common
{
    public function system()
    {
        $config = Db::name('config_system')->where(['is_hidden' => 0])->order('id asc')->select();
    
        $contain = [];
        foreach ($config as $k => $v) {
            if (in_array($v['channel'], [2])) {
                $config[$k]['default'] = json_decode($v['default'], true);
            }

            if ($v['times'] > 0) {
                $config[$k]['content'] = $v['content'] * $v['times'];
            }
        
            $contain[$v['cate_name']][] = $config[$k];
        }
    
        $config = $contain;
    
        $this->assign('data', $config);
    
        if (request()->isAjax()) {
            $post = input('post.');
            if ($post) {
                $times = Db::name('config_system')->where(['times' => ['gt', 0]])->column('title, times');

                foreach ($post as $k => $v) {
                    $v = isset($times[$k]) ? $v / $times[$k] : $v;
                    Db::name('config_system')->where(['title' => $k])->setField('content', $v);
                }
            
                $data = ['ret' => '保存成功'];
            } else {
                $data = ['err' => '提交参数错误'];
            }
        } else {
            $data = $this->fetch();
        }
        return $data;
    
    }

    public function kefu()
    {
        $where = [];

        $where['title'] = ['in', ['id']];

        $data = Db::name('config_system')->where($where)->order('id asc')->paginate(_getCachePages(), false, ['query' => request()->param()]);
        $page = $data->render();

        $this->assign('data', $data);
        $this->assign('page', $page);

        return $this->fetch();
    }

    public function kefuadd()
    {
        $id = input("id");

        if (request()->isAjax()) {

            $post = input('post.');

            /*if (!isset($post['content']) || !$post['content']) {
                return ['err' => '内容不能为空'];
            } else {*/
            $post['content'] = in_content($post['content']);
            /*}*/

            $post['is_hidden'] = 1;

            $title = Db::name('config_system')->where('id', $id)->value('title');
            if ($title == 'id') {
                $post['content'] = (int)trim(strip_tags($post['content']));
            }

            if ($id) { //添加数据
                $db = Db::name('config_system')->where('id', $id)->update($post);

                if ($db) {
                    $data = ['ret' => '修改成功'];
                } else {
                    $data = ['err' => '修改失败'];
                }
            } else {
                $post['category'] = 'kefu';
                $post['title'] = 'menu_child';
                $db = Db::name('config_system')->insert($post);

                if ($db) {
                    $data = ['ret' => '添加成功'];
                } else {
                    $data = ['err' => '添加失败'];
                }
            }
        } else {
            if ($id) {
                $name = Db::name('config_system')->where('id', $id)->find();
                if ($name) {
                    $this->assign('name', $name);
                } else {
                    $data = ['err' => 'id参数错误'];
                }
            }
            $data = $this->fetch();
        }
        return $data;
    }

    public function alipay()
    {
        $where = [];

        $search_name = input('param.name');
        $search_type = input('param.search');
        if ($search_name && $search_type) {
            $where[$search_type] = ['like', '%' . $search_name . '%'];
        }

        $types = input('types') ?: 1;
        if ($types) {
            $where['types'] = $types;
        }

        $data = Db::name('config_alipay')->where($where)->order('is_select desc, id desc')->paginate(_getCachePages(), false, ['query' => request()->param()]);

        $this->assign('data', $data);
        $page = $data->render();
        $this->assign('page', $page);

        $this->assign('types', $types);

        return $this->fetch();
    }

    public function addAlipay()
    {
        $id = input("id");

        if (request()->isAjax()) {
            $post = input('post.');

            if (!isset($post['appid']) || !$post['appid']) {
                return ['err' => 'APPID不能为空'];
            }

            if (!isset($post['private_key']) || !$post['private_key']) {
                return ['err' => '应用私钥不能为空'];
            }

            if (!isset($post['public_key']) || !$post['public_key']) {
                return ['err' => '支付宝公钥不能为空'];
            }

            if (!$id) {
                if ($post['is_select'] == 1) {
                    $data = Db::name('config_alipay')->where(['types' => $post['types'], 'is_select' => 1])->find();
                    if ($data) {
                        return ['err' => '已有选择使用的配置，请先取消已选再添加配置'];
                    }
                }

                $result = Db::name('config_alipay')->insert($post);

                $data = $result ? ['ret' => '添加成功'] : ['err' => '添加失败'];
            } else {
                if ($post['is_select'] == 1) {
                    $data = Db::name('config_alipay')->where(['id' => ['neq', $id], 'types' => $post['types'], 'is_select' => 1])->find();
                    if ($data) {
                        return ['err' => '已有选择使用的配置，请先取消已选再修改配置'];
                    }
                }

                $result = Db::name('config_alipay')->where('id', $id)->update($post);

                $data = $result ? ['ret' => '修改成功'] : ['err' => '修改失败'];
            }
        } else {
            if ($id) {
                $name = Db::name('config_alipay')->find($id);

                if ($name) {
                    $this->assign('name', $name);
                } else {
                    return ['err' => 'id参数错误'];
                }
            }
            $data = $this->fetch();
        }

        return $data;
    }

    public function delAlipay()
    {
        if (request()->isAjax()) {
            $id = input('id');
            if ($id) {
                $db = Db::name('config_alipay')->delete($id);
                if ($db) {
                    $data = ['ret' => '删除成功'];
                } else {
                    $data = ['err' => '删除失败'];
                }
            } else {
                $data = ['err' => 'id参数错误'];
            }
        } else {
            $data = ['err' => '提交参数错误'];
        }
        return $data;
    }

    public function ranking()
    {
        $where = ['types' => ['in', [1,2,3,5,6,8]]];

        $search_name = input('param.name');
        $search_type = input('param.search');
        if ($search_name && $search_type) {
            $where[$search_type] = ['like', '%' . $search_name . '%'];
        }

        $data = Db::name('config_ranking')->where($where)->order('sort desc, id asc')->paginate(_getCachePages(), false, ['query' => request()->param()]);

        $this->assign('data', $data);
        $page = $data->render();
        $this->assign('page', $page);

        return $this->fetch();
    }

    public function addRanking()
    {
        $id = input("id");

        if (request()->isAjax()) {
            $post = input('post.');
            if (!$id) {

                $result = Db::name('config_ranking')->insert($post);

                $data = $result ? ['ret' => '添加成功'] : ['err' => '添加失败'];
            } else {
                $result = Db::name('config_ranking')->where('id', $id)->update($post);

                $data = $result ? ['ret' => '修改成功'] : ['err' => '修改失败'];
            }
        } else {
            if ($id) {
                $name = Db::name('config_ranking')->find($id);

                if ($name) {
                    $this->assign('name', $name);
                } else {
                    return ['err' => 'id参数错误'];
                }
            }
            $data = $this->fetch();
        }

        return $data;
    }
    
    public function income()
    {
        $where = [];
    
        $search_name = input('param.name');
        $search_type = input('param.search');
        if ($search_name && $search_type) {
            $where[$search_type] = ['like', '%' . $search_name . '%'];
        }
    
        $types = input('types') ?: 1;
        if ($types) {
            $where['types'] = $types;
        }
    
        $data = Db::name('config_invite')->where($where)->order('min asc, id asc')->paginate(_getCachePages(), false, ['query' => request()->param()]);
    
        $this->assign('data', $data);
        $page = $data->render();
        $this->assign('page', $page);
    
        $this->assign('types', $types);
    
        return $this->fetch();
    }
    
    public function addIncome()
    {
        $id = input("id");
        
        if (request()->isAjax()) {
            $post = input('post.');
            if (!$id) {
                $result = Db::name('config_invite')->insert($post);
                
                $data = $result ? ['ret' => '添加成功'] : ['err' => '添加失败'];
            } else {
                $post['income'] = isset($post['income']) ? $post['income'] / 100 : 0;
                
                $result = Db::name('config_invite')->where('id', $id)->update($post);
                
                $data = $result ? ['ret' => '修改成功'] : ['err' => '修改失败'];
            }
        } else {
            if ($id) {
                $name = Db::name('config_invite')->find($id);
                
                if ($name) {
                    $name['income'] = $name['income'] * 100;
                    
                    $this->assign('name', $name);
                } else {
                    return ['err' => 'id参数错误'];
                }
            }
            $data = $this->fetch();
        }
        
        return $data;
    }
    public function invite_risen()
    {
            $where = [];

            $search_name = input('param.name');
            $search_type = input('param.search');
            $where['t1.value'] = ['>', 0];
            if ($search_name && $search_type) {
                if(in_array($search_type, ['id', 'phone'])){
                    $where['t2.'.$search_type] = $search_name;
                }elseif(in_array($search_type, ['name'])){
                    $where['t2.'.$search_type] = ['like', '%'.$search_name.'%'];
                }else{
                    $where['t1.'.$search_type] = ['like', '%'.$search_name.'%'];
                }
            }

            $start = input('start');
            $end = input('end');

            if ($start && $end) {
                $where['t1.time'] = ['between', [strtotime($start), strtotime($end)]];
            } else if ($start && !$end) {
                $where['t1.time'] = ['egt', strtotime($start)];
            } else if (!$start && $end) {
                $where['t1.time'] = ['elt', strtotime($end)];
            }
            $this->assign('start', $start);
            $this->assign('end', $end);

            $data = Db::name('config_invite_risen')->alias('t1')->join('user t2','t1.user_id=t2.id')->where($where)->field('t1.*,t2.name')->order('t1.id desc')->paginate(_getCachePages(), false, ['query' => request()->param()]);
            
            $this->assign('data', $data);
            $page = $data->render();
            $this->assign('page', $page);
            return $this->fetch();
    }
    
    public function editrisen()
    {
        $id = input('id');
        if (request()->isAjax()) {
            $post = input('post.');

            $user = Db::name('user')->find($post['user_id']);

            $res = false;
            $time = time();
            $result = false;
            if ($user) {
                $risen = Db::name('config_invite_risen')->where(['user_id'=>$post['user_id']])->find();
                if($risen){
                    $risened = $risen['value'];
                    $risenid = $risen['id'];
                }else{
                    $risened = 0;
                    $risenid = Db::name('config_invite_risen')->insertGetid(['user_id'=>$post['user_id'], 'create_time' => $time, 'update_time' => $time]);
                }
                if($post['value'] >= 0){
                    if ($post['value'] > 100) {
                        return ['err' => '当前可填写金额最大为 100'];
                    }
                    $res = Db::name('config_invite_risen')->where(['id'=>$risenid])->update(['value'=>$post['value'], 'update_time' => $time]);
                }else{
                    $result = ['err' => '错误值'];
                }
            }

            if ($res !== false) {
                $result = ['ret' => '操作成功'];
            } elseif(!$result) {
                $result = ['err' => '操作失败'];
            }
            return $result;
        }else{
            if ($id) {
                $name = Db::name('config_invite_risen')->where('id', $id)->find();
                if ($name) {
                    $this->assign('name', $name);
                } else {
                    $data = ['err' => 'id参数错误'];
                }
            }
            $data = $this->fetch();
        }
        return $data;
    }
}